package com.dy.字符串.最长回文子串;

import java.util.ArrayList;
import java.util.List;

/*
最长回文子串
给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

示例 1：

输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
示例 2：

输入: "cbbd"
输出: "bb"
 */
public class Solution {
    //动态规划 dp[i][j]=1表示i到j是回文串
    public static String longestPalindrome(String s) {
        if (s.isEmpty()) {
            return "";
        }
        int len = s.length();
        int longest = 1;
        int start = 0;
        boolean dp[][] = new boolean[len][len];
        ;
        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
            if (i < len - 1) {
                if (s.charAt(i) == s.charAt(i + 1)) {
                    dp[i][i + 1] = true;
                    longest = 2;
                    start = i;
                }
            }
        }
        for (int length = 3; length <= len; length++) {
            for (int i = 0; i + length - 1 < len; i++) {
                int j = i + length - 1;
                if (s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1] == true) {
                    dp[i][j] = true;
                    start = i;
                    longest = length;
                }
            }
        }
        return s.substring(start, start + longest);


    }

    /*
    aaabcba
    0123456
     */
    //中心扩展法，分奇数个和偶数个
    public static String longestPalindrome2(String s) {
        if (s == null || s.length() == 0) return "";
        int start = 0, end = 0;
        for (int i = 0; i < s.length(); i++) {
            int len1 = expandAroundCenter(s, i, i);
            int len2 = expandAroundCenter(s, i, i + 1);
            int len = Math.max(len1, len2);
            if (len > end - start + 1) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }
        return s.substring(start, end + 1);

    }

    private static int expandAroundCenter(String s, int left, int right) {
        while (left >= 0 && right <= s.length() - 1 && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        return right - left - 1;
    }

    /*
    Manacher算法
    P[i]表示以i为中心，右半部分的长度，这个长度就是原始回文串的长度
    P[i]由间隔的分隔符加成
     */
    public static String longestPalindrome3(String s) {
        if (s.isEmpty()) return "";
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            stringBuilder.append("#");
            stringBuilder.append(s.charAt(i));
        }
        stringBuilder.append("#");
        String newString = stringBuilder.toString();

        int pLen = newString.length();
        int[] p = new int[pLen];
        int center = 0, right = 0;
        for (int i = 1; i < pLen; i++) {
            int mirror = 2 * center - i;

            p[i] = (right > i) ? Math.min(p[mirror], right - i) : 0;

            while (i - p[i] - 1 >= 0 && i + p[i] + 1 < newString.length() && newString.charAt(i + p[i] + 1) == newString.charAt(i - p[i] - 1)) {
                p[i]++;
            }
            if (i + p[i] > right) {
                center = i;
                right = i + p[i];
            }
        }
        int maxLen = 0;
        int centerIndex = 0;
        for (int i = 1; i < pLen; i++) {
            if (p[i] > maxLen) {
                maxLen = p[i];
                centerIndex = i;
            }
        }
        centerIndex = centerIndex / 2 - (maxLen) / 2; //start
        return s.substring(centerIndex,centerIndex + maxLen);
        //return s.substring(centerIndex / 2 - (maxLen) / 2, centerIndex / 2+(maxLen-1)/2 + 1);
    }


}
